﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CommonsLib.Utility
{
    public class CvtFunc
    {
        public static string ToString(object obj)
        {
            return ToString(obj, true);
        }
        private static string ToString(object obj, bool blnTrimEnd)
        {
            if (obj == null) return "";

            string strTo = obj.ToString();
            if (blnTrimEnd)
                strTo = strTo.TrimEnd();

            return strTo;
        }

        public static string ToLeftString(object obj, int iLength)
        {
            return ToLeftString(obj, iLength, true);
        }
        private static string ToLeftString(object obj, int iLength, bool blnTrimEnd)
        {
            if (obj == null) return "";
            if (iLength <= 0) return string.Empty;
            string strTo = ToString(obj, blnTrimEnd);
            if (strTo.Length > iLength)
                strTo = strTo.Substring(0, iLength);

            if (blnTrimEnd)
                strTo = strTo.TrimEnd();

            return strTo;

        }
        public static string ToRightString(object obj, int iLength)
        {
            return ToRightString(obj, iLength, true);
        }
        private static string ToRightString(object obj, int iLength, bool blnTrimEnd)
        {
            if (obj == null) return "";
            if (iLength <= 0) return string.Empty;
            string strTo = ToString(obj, blnTrimEnd);
            if (strTo.Length > iLength)
                strTo = strTo.Substring(strTo.Length - iLength);

            if (blnTrimEnd)
                strTo = strTo.TrimEnd();

            return strTo;
        }
        public static string ToSubstring(object obj, int iStartIndex)
        {
            string str = CvtFunc.ToString(obj);
            return ToSubstring(str, iStartIndex, str.Length, true);
        }
        public static string ToSubstring(object obj, int iStartIndex, int iLength)
        {
            return ToSubstring(obj, iStartIndex, iLength, true);
        }
        private static string ToSubstring(object obj, int iStartIndex, int iLength, bool blnTrimEnd)
        {
            if (obj == null) return "";
            if (iStartIndex < 0) return string.Empty;
            if (iLength <= 0) return string.Empty;
            string strTo = ToString(obj, blnTrimEnd);
            if (iStartIndex > strTo.Length || iStartIndex < 0 || iLength < 0)
            {
                return "";
            }
            else if (iStartIndex + iLength > strTo.Length)
            {
                strTo = strTo.Substring(iStartIndex);
            }
            else
                strTo = strTo.Substring(iStartIndex, iLength);

            if (blnTrimEnd)
                strTo = strTo.TrimEnd();

            return strTo;
        }


        public static string ToDateString(object obj)
        {
            if (obj == null || obj.ToString().TrimEnd() == "") return "";

            return BaseFunctions.FormatDate(obj.ToString());
        }

        public static DateTime ToDateTime(object obj)
        {
            if (obj == null || obj.ToString().TrimEnd() == "" || obj.ToString().Trim() == "/  /") return new DateTime(1900, 1, 1);

            return DateTime.Parse(obj.ToString());
        }

        public static double ToDouble(object obj)
        {
            return ToDouble(obj, -1);
        }
        public static double ToDouble(object obj, int intDecimalPoint)
        {
            string strFormatStyle = "";

            if (obj == null || obj.ToString().TrimEnd() == "") return 0;

            if (intDecimalPoint >= 0)
            {
                strFormatStyle = "N." + intDecimalPoint.ToString();
                return double.Parse(BaseFunctions.FormatNumeric(obj.ToString(), strFormatStyle));
            }
            else
            {
                //return double.Parse(obj.ToString());

                return double.Parse(BaseFunctions.FormatNumeric(obj, "n.n"));

            }
        }

        public static int ToInt(object obj)  //Int32
        {
            if (obj == null || obj.ToString().TrimEnd() == "" || !BaseFunctions.IsNumeric(obj.ToString().Trim())) return 0;

            return int.Parse(BaseFunctions.FormatNumeric(obj, "n.0"));
        }

        public static long ToLong(object obj) //Int64
        {
            if (obj == null || obj.ToString().TrimEnd() == "") return 0;

            return long.Parse(BaseFunctions.FormatNumeric(obj, "n.0"));
        }

        public static bool ToBoolean(object obj)
        {
            if (obj == null || obj.ToString().TrimEnd() == "") return false;

            if (obj.ToString().ToUpper() == "YES") return true;
            if (obj.ToString().ToUpper() == "NO") return false;
            if (obj.ToString().ToUpper() == "1") return true;
            if (obj.ToString().ToUpper() == "0") return false;

            return Convert.ToBoolean(obj.ToString());
        }

        /// <summary>
        /// 将Unix时间戳转换为DateTime类型时间
        /// </summary>
        /// <param name="intUnixStamp">int 型数字</param>
        /// <returns>DateTime</returns>
        public static System.DateTime ToDateTime(long intUnixStamp)
        {
            System.DateTime time = System.DateTime.MinValue;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            time = startTime.AddSeconds(intUnixStamp  );
            return time;
        }

        /// <summary>
        /// 将c# DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time">时间</param>
        /// <returns>int</returns>
        public static long ToUnitStamp(System.DateTime time)
        {
            long intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            intResult =CvtFunc.ToLong( (time - startTime).TotalSeconds);
            
            return intResult ;
        }




    }
}